{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "### 运行MongoDB：\n",
    "创建数据目录：启动MongoDB之前，首先要给他指定一个数据存储的路径。比如我在MongoDB的安装路径下创建一个data文件夹，专门用来存储数据的D:\\ProgramApp\\mongodb\\data。\n",
    "把mongodb的bin目录加入到环境变量中。方便后期调用。\n",
    "命令执行mongod --dbpath D:\\ProgramApp\\mongodb\\data启动。\n",
    "MongoDB的基本操作命令：\n",
    "db：查看当前的数据库。\n",
    "show dbs：查看所有的数据库。\n",
    "use 数据库名：。切换数据库如果数据库不存在，则创建一个（创建完成后需要插入数据库才算创建成功）\n",
    "db.dropDatabase()：删除当前指向的数据库。\n",
    "db.集合名.insert(value)：添加数据到指定的集合中。\n",
    "db.集合名.find()：从指定的集合中查找数据。更多命令请见：http：//www.runoob.com/mongodb/mongodb-tutorial.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pymongo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "client = pymongo.MongoClient(\"127.0.0.1\",port=27017)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "db = client.zhihu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pymongo.results.InsertManyResult at 0x7678af8>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#增删改查\n",
    "#插入数据\n",
    "collection=db.qa\n",
    "collection.insert_many([{\n",
    "    'username':'bbb', \"age\":'19'\n",
    "},{\n",
    "    'username':'bdd',    \"age\":'12'\n",
    "},{\n",
    "    'username':'dryao',    \"age\":'44'\n",
    "}]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'_id': ObjectId('5cdaa394b37331e3b6a97524'), 'username': 'aaa', 'age': '18'}\n",
      "{'_id': ObjectId('5cdaa6235aeac9b584e88dee'), 'username': 'aaaccc'}\n",
      "{'_id': ObjectId('5cdaa7bd5aeac9b584e88df0'), 'username': 'bbb', 'age': '19'}\n",
      "{'_id': ObjectId('5cdaa7bd5aeac9b584e88df1'), 'username': 'bdd', 'age': '12'}\n",
      "{'_id': ObjectId('5cdaa7ec5aeac9b584e88df2'), 'username': 'bbb', 'age': '19'}\n",
      "{'_id': ObjectId('5cdaa7ec5aeac9b584e88df3'), 'username': 'bdd', 'age': '12'}\n",
      "{'_id': ObjectId('5cdaa7ec5aeac9b584e88df4'), 'username': 'dryao', 'age': '44'}\n",
      "{'_id': ObjectId('5cdaa7bd5aeac9b584e88df1'), 'username': 'bdd', 'age': '12'}\n"
     ]
    }
   ],
   "source": [
    "#查找数据\n",
    "cursor = collection.find()\n",
    "for x in cursor:\n",
    "    print(x)\n",
    "cursor2=collection.find_one({\"age\":'12'})\n",
    "print(cursor2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pymongo.results.UpdateResult at 0x7678cd8>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#更改数据\n",
    "collection.update_one({\"username\":\"bbb\"},{\"$set\":{\"username\":\"bodeee\"}})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "#装饰器的理解\n",
    "def logger(func):\n",
    "  \n",
    "    \n",
    "    def inner(*args, **kwargs):#1\n",
    "            print('我是inner')\n",
    "            return func(*args, **kwargs)#2\n",
    "    print('我是Logger')\n",
    "    return inner\n",
    "\n",
    "def loggr2(func):\n",
    "  \n",
    "    \n",
    "    def inner(*args, **kwargs):#1\n",
    "            print('我是inner2')\n",
    "            return func(*args, **kwargs)#2\n",
    "    print('我是Logger2')\n",
    "    return inner\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是Logger2\n",
      "我是Logger\n"
     ]
    }
   ],
   "source": [
    "@logger\n",
    "@loggr2\n",
    "def add(x,y):\n",
    "    print('我是ADD')\n",
    "    return x+y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是inner\n",
      "我是ADD\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "add(5,6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
